package com.gskj.educenter.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.gskj.commonutils.JwtUtils;
import com.gskj.commonutils.ResultJson;
import com.gskj.commonutils.orderVO.UcenterMemberOrder;
import com.gskj.educenter.entity.MemberRegisterVO;
import com.gskj.educenter.entity.UcenterMember;
import com.gskj.educenter.service.IUcenterMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Connor-G
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
//@Slf4j
public class UcenterMemberController {

    @Autowired
    private IUcenterMemberService memberService;

    /***
     *  会员登陆请求
     * @param member
     * @return
     */
    @PostMapping("/login")
    public ResultJson memberLogin(@RequestBody UcenterMember member) {

        // service
        // 返回tolen值
        String token = memberService.login(member);
//        log.info("----------token--  " + token);

        return ResultJson.success().data("token", token);
    }

    /***
     *  会员注册
     * @param member
     * @return
     */
    @PostMapping("/register")
    public ResultJson registerMember(@RequestBody MemberRegisterVO member) {

        memberService.registerMember(member);
        return ResultJson.success();
    }

    /***
     * TODO  使用远程接口调用，参数为Request对象。会报异常。暂时无法解决！！！
     *   将request中的header中token解析得到会员id
     * @return
     */
    @GetMapping("/get/memberinfo")
    public ResultJson parseToken(HttpServletRequest request) {

//        log.info("token  " + request.getHeader("token"));
        // 获取 会员id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        // 根据 会员id查询
        UcenterMember member = memberService.getById(memberId);

        // 将查询的会员 返回到前台
        return ResultJson.success().data("memberInfo", member);
    }

    /***
     *  根据 token 字符串、获取 会员
     * @param token
     * @return
     */
    @GetMapping("/get/memberinfo/str/{token}")
    public ResultJson parseTokenWithStr(@PathVariable("token") String token) {

        // 获取 会员id
        String memberId = JwtUtils.getMemberIdByJwtTokenStr(token);

        // 根据 会员id查询
        UcenterMember member = memberService.getById(memberId);

        // 将查询的会员 返回到前台
        return ResultJson.success().data("memberInfo", member);
    }

    /**
     * 通过 会员id 获取 会员信息。为order模块调用
     *
     * @param id
     * @return
     */
    @GetMapping("/get/memberInfoForOrder/{id}")
    public UcenterMemberOrder getMemberById(@PathVariable("id") String id) {

        // 1. 根据会员id 获取
        UcenterMember byId = memberService.getById(id);

        // 2. 通过 BeanUtils 赋值
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(byId, ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    /***
     *  根据 当前日期 查询 当前日期注册的人数总和
     * @param day
     * @return
     */
    @GetMapping("/countRegister/{day}")
    public ResultJson countRegister(@PathVariable("day") String day) {

        int count = memberService.countRegisterDay(day);
        return ResultJson.success().data("register", count);
    }

}

